package com.pkk.components.dynamicdb.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.pkk.components.dynamicdb.constants.DataSourceContants;
import com.pkk.components.dynamicdb.dynamic.DynamicDataSource;
import com.pkk.components.dynamicdb.util.DataSourceSet;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @description: 多数据源配置
 * @author: peikunkun
 * @create: 2018-11-24 16:53
 **/
@Configuration
public class DynamicDataSourceConfig {

  @Bean
  @ConfigurationProperties("spring.datasource.druid")
  public DataSource mainDataSource() {
    return DruidDataSourceBuilder.create().build();
  }

  @Bean("dataSource")
  @Primary
  public DynamicDataSource dataSource(DataSource dataSource) {
    DataSourceSet.putTargetDataSourcesMap(DataSourceContants.MAINDATASOURCE, dataSource);
    return new DynamicDataSource(dataSource, DataSourceSet.getTargetDataSourcesMap());
  }

}
